<?php

namespace app\api\service;

use app\api\validate\AddressValidate;
use support\spark\exception\ApiException;
use support\spark\model\user\UserAddress;

class AddressService
{
    /**
     * 获取默认地址
     * @param $userId
     * @return array
     */
    public function getDefaultAddress($userId)
    {
        $userAddressModel = new UserAddress();
        $addressInfo = $userAddressModel->getInfoByWhere([
            'user_id' => $userId,
            'is_del' => 1,
            'is_default' => 1
        ]);

        return dataReturn(0, 'success', $addressInfo);
    }

    /**
     * 获取用户的地址
     * @param $userId
     * @return array
     */
    public function getUserAddressList($userId)
    {
        $userAddressModel = new UserAddress();
        $addressList = $userAddressModel->getAllList([
            'user_id' => $userId,
            'is_del' => 1
        ], ['*'], ['is_default' => 'asc', 'id' => 'desc']);

        return dataReturn(0, 'success', $addressList);
    }

    /**
     * 添加用户地址
     * @param $param
     * @param $userId
     * @return array
     */
    public function addUserAddress($param, $userId)
    {
        $validate = new AddressValidate();
        if (!$validate->check($param)) {
            throw  new  ApiException($validate->getError());
        }

        $param['create_time'] = now();
        $param['user_id'] = $userId;
        $param['is_default']=$param['is_default']??1;
        $userAddressModel = new UserAddress();
        if ($param['is_default'] == 1) {
            $userAddressModel->updateByWhere([
                'is_default' => 2
            ], ['user_id' => $param['user_id']]);
        }
        $res= $userAddressModel->insertOne($param);
        return  dataReturn(0,'添加成功',[$res]);
    }

    /**
     * 编辑用户地址
     * @param $param
     * @param $userId
     * @return array
     */
    public function editUserAddress($param, $userId)
    {
        $validate = new AddressValidate();
        if (!$validate->check($param)) {
            throw  new  ApiException($validate->getError());
        }
        $param['update_time'] = now();
        $param['user_id'] = $userId;

        $userAddressModel = new UserAddress();
        if ($param['is_default'] == 1) {
            $userAddressModel->updateByWhere([
                'is_default' => 2
            ], ['user_id' => $param['user_id']]);
        }

         $userAddressModel->updateByWhere($param, [
            'id' => $param['id'],
            'user_id' => $userId
        ]);
        return  dataReturn(0,'编辑成功');
    }

    /**
     * 删除用户地址
     * @param $id
     * @param $userId
     * @return array
     */
    public function delUserAddress($id, $userId)
    {
        $userAddressModel = new UserAddress();
        $userAddressModel->delByWhere([
            'id' => $id,
            'user_id' => $userId
        ]);
        return  dataReturn(0,'删除成功');
    }

    /**
     * 设置默认地址
     * @param $id
     * @param $userId
     * @return array
     */
    public function setDefault($id, $userId)
    {
        $userAddressModel = new UserAddress();
        $userAddressModel->updateByWhere(['is_default' => 2], [
            'user_id' => $userId
        ]);

        $userAddressModel->updateByWhere(['is_default' => 1], [
            'user_id' => $userId,
            'id' => $id
        ]);

        return dataReturn(0);
    }
}